from rest_framework import serializers

from system import models


class MenuSerializers(serializers.ModelSerializer):
    children = serializers.SerializerMethodField()

    def get_children(self, obj):
        childMenu = models.Menu.objects.filter(pid=obj.id)
        if childMenu:
            return MenuSerializers(childMenu, many=True).data
        return None

    class Meta:
        model = models.Menu
        fields = ['id', 'name', 'children', 'url', 'component','icon', 'type',"pid"]
